/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nrechten
 */
public class ProximityTween extends Tween {

    protected float radius = 1;
    protected float outer = 1;
    protected ProximityGroup group;
    protected ProximityGroup touching;
    protected List<ProximityTween> direct = new ArrayList<ProximityTween>();


    protected int links;

    public ProximityTween(float[] target, ProximityGroup group) {
        super(target);
        this.group = group;
        group.add(this);
    }

    @Override
    public void step() {
        calcDir();
        super.step();
    }

    public void calcDir(){
        //start with how far we are from our target
        dir = new Vector3f(target);
        dir.sub(current);
        remaining = new Vector3f(dir);
        dir.normalize();

//        Vector3f proxVector = new Vector3f();

        for (ProximityTween pt : direct) {
            dir.add(pt.calcVector(this));
        }

//        System.out.println(dir);

    }

    public Vector3f calcVector(ProximityTween pt){
        Vector3f t = new Vector3f(current);
        if(t.equals(pt.current)){
            if(pt.dir.length() == 0){
                t = new Vector3f((float)Math.random(),(float)Math.random(),0);
            } else {
                t = new Vector3f(pt.dir);
                t.negate();
            }
        } else {
            t.sub(pt.current);
        }
//        t.scale((pt.radius+radius)/t.length());
        //System.out.println(t);
        return t;
    }

    public boolean check(ProximityTween other){

        Vector3f temp = new Vector3f(current);
        temp.sub(other.current);

        return (radius+other.radius)>temp.length();
    }

    public void setTouching(ProximityGroup touching) {
        this.touching = touching;
        if(this.touching != null){
            this.touching.add(this);
        }

    }

    @Override
    public boolean testFinished() {
        if(direct.size() > 0) return false;
        return super.testFinished();
    }



    public ProximityGroup getTouching() {
        return touching;
    }

    public int getLinks() {
        return links;
    }

    public void setLinks(int links) {
        this.links = links;
    }


    public void resetLinks(){
        this.links = 0;
    }

    public void incLinks(){
        this.links++;
    }

    public void clearDirect(){
        direct.clear();
    }

    public void addDirect(ProximityTween pt){
        direct.add(pt);
    }

}
